package com.scitc.controller;

import com.scitc.dto.userDto.ChangeUserDto;
import com.scitc.dto.userDto.LoginDto;
import com.scitc.dto.userDto.RegisterDto;
import com.scitc.model.Result;
import com.scitc.model.User;
import com.scitc.service.UserService;
import com.scitc.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

/**
 * @author: zk
 * @date: 2021/5/6 1:20
 * @description:
 */
@RestController
public class UserController {

    @Autowired
    private JwtUtil jwtUtil;

    @Autowired
    private UserService userService;

    @PostMapping("/login")
    private Result login(@RequestBody LoginDto loginDto) {
        String password = loginDto.getPassword();
        boolean isRemember = loginDto.isRemember();

        User user = userService.getUserByUsername(loginDto.getUsername());

        if(user == null) return Result.fail("未找到该用户");

        if(user.isRemember() == true || user.getPassword().equals(password)) { // 登陆成功
            // 是否记住密码
            if(isRemember == true) {
                userService.setUserRememberById(user.getId(), true);
            }else{
                userService.setUserRememberById(user.getId(), false);
            }
            // 返回 token
            return Result.success("登录成功", jwtUtil.createToken(user));
        }else{ // 登陆失败
            return Result.fail("密码错误");
        }
    }

    @PostMapping("/register")
    private Result register(@RequestBody RegisterDto registerDto) {
        String username = registerDto.getUsername();
        String password = registerDto.getPassword();

        if(userService.getUserByUsername(username) != null)
            return Result.fail("用户名已存在");
        String id = UUID.randomUUID().toString();
        User user = new User(
                id,
                username,
                password,
                false,
                username,
                false);
        userService.addUser(user);

        return Result.success("注册成功", "注册成功");
    }

    @PostMapping("/changeuser")
    private Result changeUser(@RequestBody ChangeUserDto changeUserDto) {
        String token = changeUserDto.getToken();
        String password = changeUserDto.getPassword();
        String newPassword = changeUserDto.getNewPassword();
        String name = changeUserDto.getName();

        if(token == null) return Result.fail("请先登录");

        switch (jwtUtil.verify(token)) {
            case 2: return Result.fail("会话超时请重新登录");
            case 0: return Result.fail("令牌无效");
        }

        String id = (String) jwtUtil.analysis(token).get("id");
        User user = userService.getUserById(id);

        // 判断密码是否正确
        if(password.equals(user.getPassword()) == false) {
            return Result.fail("密码错误");
        }

        if(name != null)
            userService.changeNameById(id, name);
        if(newPassword != null)
            userService.changePasswordById(id, newPassword);

        return Result.success("操作成功", "操作成功");
    }
}
